package org.amphiprion.rfreplayanalyser.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class ErrorDialog extends JDialog {
	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("org.amphiprion.rfreplayanalyser.ui.messages"); //$NON-NLS-1$

	private final JPanel contentPanel = new JPanel();
	private JLabel lblErrorMessage = new JLabel();
	private JScrollPane scrollPane = new JScrollPane();
	private JEditorPane editorPane = new JEditorPane();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ErrorDialog dialog = new ErrorDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ErrorDialog() {
		setTitle(BUNDLE.getString("ErrorDialog.this.title")); //$NON-NLS-1$
		setBounds(100, 100, 852, 301);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{

			lblErrorMessage.setText("...");
			contentPanel.add(lblErrorMessage, BorderLayout.NORTH);
		}
		{

			contentPanel.add(scrollPane, BorderLayout.CENTER);
			{

				editorPane.setEditable(false);
				scrollPane.setViewportView(editorPane);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						ErrorDialog.this.setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	public static void showError(String message, Component parent) {
		ErrorDialog dlg = new ErrorDialog();
		dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dlg.setModal(true);
		dlg.lblErrorMessage.setText(message);
		dlg.scrollPane.setVisible(false);
		dlg.pack();
		dlg.setLocationRelativeTo(parent);
		dlg.setVisible(true);

	}

	public static void showError(Throwable t, Component parent) {
		ErrorDialog dlg = new ErrorDialog();
		dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dlg.setModal(true);
		dlg.lblErrorMessage.setText(t.getMessage());
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		t.printStackTrace(pw);
		dlg.editorPane.setText(sw.toString());

		dlg.setLocationRelativeTo(parent);
		dlg.setVisible(true);
	}
}
